<?php


namespace app\admin\model;


use think\Db;

class OrderDepositModel extends BaseModel
{
    protected $name='order_deposit';
    protected $pk='id';

    protected $autoWriteTimestamp = true;
    protected $updateTime = false;
    protected $createTime = 'add_time';
    public function doAddData($data=[]){
        $order_id = $data['order_id'];
        $order_info = Db::name('order')->alias('o')->field('o.*,p.tomb_region,p.tomb_address')
            ->join("dj_tombarea_information_product p","o.tomb_product_id = p.id","left")
            ->where(['o.order_id'=>$order_id])->find();

        $customer_id = $data['customer_id'];
        $this->startTrans();
        try {
            $this_is = $this->where([ 'mw_id' => $data['tomb_id'],'order_id' => $order_id,'customer_id' => $data['customer_id']])->find();
            $this_is_sd = ServiceDepositModel::where([ 'tomb_id'=>$data['tomb_id'],'service_status' => 1,'submit_status' => 3,'customer_id' => $data['customer_id']])->find();

            $deposit_num = $this->get_deposit_num();
            $data_arr = [
                'mw_id' => $data['tomb_id'],
                'mw_num' => $data['tomb_num'],
                'order_id' => $data['order_id'],
                'customer_id' => $data['customer_id'],
                'deposit_time' => time(),
                'storage_time' => $data['deposit_storage'],
                'deposit_end_time' => unixtime_to_date_inc(time(), intval($data['deposit_storage'])),
                'original_price' => $data['selling_price'],
                'discount_duration' => $data['discount_duration'],
                'pay_type' => $data['pay_type'],
                'total_price' => $data['total_price'],
                'handle_name' => cmf_get_admin_name(),
            ];
            if (!empty($this_is)){
                $this->update($data_arr,[ 'id' => $this_is['id']]);
                $deposit_id = $this_is['id'];
            }else{
                $data_arr['deposit_num'] = $deposit_num;
                $info = $this->create($data_arr);
                $deposit_id = $info['id'];
            }
            $shezhe_info = ArchivesDeceasedModel::where(['customer_id'=>$customer_id])->find();
            $goumu_info = CustomerInfoModel::where(['id'=>$customer_id])->find();

            $deceased_id = $shezhe_info['deceased_id'];
            //
            $services['deposit_id'] = $deposit_id;
            $services['tomb_id'] = $data['tomb_id'];
            $services['tomb_num'] = $data['tomb_num'];
            $services['deposit_time'] =time();
            $services['customer_id'] = $customer_id;
            $services['tomb_address'] = $order_info['tomb_region'] . $order_info['tomb_address'];
            $services['dealer_user'] = $goumu_info['buyer_name']; // 办理人
            $services['dealer_sex'] = $goumu_info['buyer_sex']; //
            $services['dealer_phone'] = $goumu_info['buyer_phone']; // 办理人
            $services['dealer_cart_num'] = $goumu_info['customer_account']; // 办理人
            $services['dealer_cart_type'] = $goumu_info['cart_type']; // 办理人
            $services['dealer_address'] = $goumu_info['customer_address']; // 办理人
            $services['relationship'] = $shezhe_info['relationship_users']; // 关系
            $services['url_cart_file'] = $goumu_info['cart_upload']; // 办理人
            $services['handle_id'] = cmf_get_current_admin_id();
            $services['handle_name'] = cmf_get_admin_name();
            $services['handle_id'] = cmf_get_current_admin_id();
            $services['buyer_name'] = $goumu_info['buyer_name'];
            $services['dead_name'] = $shezhe_info['deceased_name'];
            $services['service_status'] = 1;
            $services['submit_status'] =3;
            $services['add_time'] = time();
            $services['add_user'] = cmf_get_current_admin_id();

            if (!empty($this_is_sd)){
                ServiceDepositModel::update($services,[ 'id'=>$this_is_sd['id']]);
                $serinsert_id = $this_is_sd['id'];
            }else{
                $serinsert_id = ServiceDepositModel::insertGetId($services);
            }

            $death['servicedeposit_id'] = $serinsert_id;
            $death['service_jc_id'] = $serinsert_id;
            $death['move_status'] =1;
            $death['deposit_status'] = 0;
            ArchivesDeceasedModel::update($death, ['deceased_id' => $deceased_id]);
            $pay_ins = [
                'type' => 2,
                'itype' => 1,//
                'order_id' => $data['order_id'],
                'other_id' => $serinsert_id,
                'tomb_id' => $data['tomb_id'],
                'tomb_num' => $data['tomb_num'],
                'tom_price' => $data['selling_price'],//购墓价格
                'total_price' => $data['deal_price'],//
                'deal_price' => $data['deal_price'],//应收金额
                'real_name' => $data['real_name'],//付款人
                'real_phone' => $data['real_phone'],//付款人
                'pay_type' => $data['pay_type'],//付款方式
                'remark' => $data['remark'],//备注
                'discount_duration' => $data['discount_price'],
                'add_time' => time(),//备注
                'handle' => cmf_get_current_admin_id(),//收款人
                'pay_real_name' => cmf_get_current_admin_id(),//收款人
                'status' => 0,// 付款方式
                'customer_id' => $data['customer_id'],
            ];
            if (!empty($data['discount_price'])) {
                $pay_ins['free_price'] = $data['selling_price'] * $data['discount_price'];
            }

            $this_is_pl = OrderPayLogModel::where([ 'type' => 2,'itype' => 1,'order_id' => $data['order_id']])->find();
            if (!empty($this_is_pl)){
                OrderPayLogModel::update($pay_ins,[ 'id' => $this_is_pl['id']]);
                $pay_id =  $this_is_pl['id'];
            }else{
                $pay_ins['pay_sn']=  OrderPayLogModel::getselfPayNum('add_time','pay_sn','JC-');
                $rets = OrderPayLogModel::insertGetId($pay_ins);
                $pay_id = $rets;
            }

            $this->update(['pay_id'=>$pay_id],['id'=>$deposit_id]);
            ServiceDepositModel::update(['pay_id'=>$pay_id],['id'=>$serinsert_id]);
            ArchivesDeceasedModel::update(['deposit_id' => $deposit_id], ['customer_id' => $data_arr['customer_id']]);
            $this->commit();
            return true;
        }catch(\Exception $exception){
            $this->error='添加失败';
            $this->rollback();
            return false;
        }

    }


    public function  get_deposit_num(){
        $count = $this->order('id DESC')->find();
        $time =date('Y');
        if(date('Y',$count['add_time'])==$time){
            $num = 'JJZ-370126-'.$time.'-'.str_pad(substr($count['deposit_num'],-7)+1, 4, '0', STR_PAD_LEFT);
        }else{
            $num ='JJZ-370126-'.$time.'-1'.str_pad(1, 6, '0', STR_PAD_LEFT);;
        }
        return $num;
    }

}